import { Component, OnInit } from "@angular/core";
import { _HttpClient } from "@delon/theme";
import { DatePipe } from '@angular/common';
import { LocalStorage } from "@core/local.storage";
import { StatusFollowService } from '@core/statusFollow.service';
import { JzjcMainService } from './jzjc_main.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { forkJoin } from 'rxjs';
import { JzjcTool } from './jzjc_main.tool';
import { ActivatedRoute } from '@angular/router';
import { ReuseTabService } from '@delon/abc';
import { ExportFileService } from '@core/exportFile';
import { environment } from '../../../../../environments/environment';

@Component({
  selector: "app-tctj-day",
  templateUrl: "./jzjc_main.component.html",
  styleUrls: ["./jzjc_main.component.less", "../jzjc.less"]
})
export class JzjcMainComponent implements OnInit {

  userInfo = JSON.parse(this.ls.get('loginInfo'));
  queryParam: any = {};
  tableData: any[] = [];
  tableInfo: any = {};
  tableLoading = false;
  aboveTable = [320, 320, 340];
  lockHeadScroll = { x: '2200px', y: `${this.getHeight()}px` };
  pagination: any = {
    selectedValue: '100',
    pageTotal: 0,
    pageIndex: 1,
    selectedChange: () => {
      this.query();
    },
    pageIndexChange: () => {
      this.query();
    },
  };
  exportTitle = '';
  modal: any = {
    visible: false,
    title: '',
    nzBodyStyle: { width: '60vw', minWidth: '800px' },
  }
  detail: any = {};
  btnState: any = {};
  optConfig: any = {
  }

  confirmParam: any = {
    applyrectdate: "",
    handleinfo: "",
  }
  tabs: any = [];
  bmList: any[] = [];
  wtlxList: any[] = [];
  statusArr: any[] = [];
  // showQueryCmp = true;


  fxtTitle = {
    hkgst: '沪昆高速台',
    nkkzt: '南昆客专台',
    nkhgpst: '南昆、红果普速台',
    hkpst: '沪昆普速台',
    all: '监测问题汇总表',
  }
  screenType = "";
  tableClass: any = {};
  czList: any = [];
  czList_filter: any = [];
  // fxtType: any = "";

  constructor(
    private activatedRoute: ActivatedRoute,
    private modalService: NzModalService,
    private datePipe: DatePipe,
    public msg: NzMessageService,
    private reuseTabService: ReuseTabService,
    private exportFile: ExportFileService,
    public ls: LocalStorage,
    private jzjcMainService: JzjcMainService,
    private statusFollowService: StatusFollowService,
    public jzjcTool: JzjcTool
  ) { }

  ngOnInit(): void {
    this.activatedRoute.params.subscribe(params => {
      this.queryParam.pc = params.type;
      this.judgeScreen();
      this.initTableStyle();
      this.lockHeadScroll.y = `${this.getHeight()}px`;
      this.initCmp(params);
    });
  }

  initTableStyle() {
    this.tableClass = {
      tableHeight1: this.screenType === '1',
      tableHeight2: this.screenType === '2',
      tableHeight3: this.screenType === '3',
    }
  }

  getHeight() {
    const index = this.screenType ? `${+this.screenType - 1}` : "0";
    return window.innerHeight - this.aboveTable[index];
  }

  initCmp(params) {
    this.initTab();
    this.jzjcTool.initCmp();
    this.getCz(params.type);
    // this.initBtnState();
    this.initOptConfig();
    this.reuseTabService.title = this.fxtTitle[params.type];
    this.exportTitle = this.fxtTitle[params.type];
    this.onTabIndexChange(0);
  }

  getCz(pc, func?) {
    if (!pc) {
      return;
    }
    this.jzjcMainService.getStationByjcpc(pc).subscribe(res => {
      if (res && res.data && res.data.length) {
        this.czList = res.data[0];
      }
      func && func();
    });
  }

  onChange_cz(v) {
    this.czList_filter = v ? this.czList.filter(el => el.STATIONNAME.includes(v)) : this.czList;
  }


  /**
   * tabId 对应tab页的tabtype的值
   */
  initTab() {
    if (this.queryParam.pc === 'all') {
      this.tabs.push({
        title: '未整改',
        tabId: "3",
      }, {
        title: '已销号',
        tabId: "4",
      });
      if (this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40') {
        this.tabs.unshift({
          title: '待处理',
          tabId: "5",
        })
        this.tabs.push({
          title: '汇总表',
          tabId: "6",
        });
      }
      this.queryParam.tabtype = "5";
    } else {
      if (this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40') {
        this.tabs.push({
          title: '待处理',
          tabId: "5",
        }, {
          title: '未整改',
          tabId: "3",
        }, {
          title: '已销号',
          tabId: "4",
        });
        this.queryParam.tabtype = "5";
      } else if (this.userInfo.N_LEVEL === 30 || this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
        this.tabs = [{
          title: '待处理',
          tabId: "1",
        },
        // {
        //   title: '本部门协助',
        //   tabId: "2",
        // }, 
        {
          title: '未整改',
          tabId: "3",
        }, {
          title: '已销号',
          tabId: "4",
        }];
        this.queryParam.tabtype = "1";
      } else {
        this.tabs = [{
          title: '本部门负责',
          tabId: "7",
        },
        {
          title: '未整改',
          tabId: "3",
        }, {
          title: '已销号',
          tabId: "4",
        }];
        this.queryParam.tabtype = "7";
      }
    }
  }

  initBtnState() {
    if (this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40') { // 调度 不管是汇总还是工作台都要能操作
      this.btnState.deletable = true
      this.btnState.addable = true
      this.btnState.agreeable = true
      this.btnState.disagreeable = true
      this.btnState.qianshou = false
      this.btnState.shangbao = false
      this.btnState.sqYanqi = false
      this.btnState.sqXiaohao = false
      this.btnState.xiafa = true
    } else {
      if (this.queryParam.pc === 'all') { // 调度以外的用户 如果是汇总 里面不需要操作按钮
        this.btnState.deletable = false
        this.btnState.addable = false
        this.btnState.agreeable = false
        this.btnState.disagreeable = false
        this.btnState.qianshou = false
        this.btnState.shangbao = false
        this.btnState.sqYanqi = false
        this.btnState.sqXiaohao = false
        this.btnState.xiafa = false
        return;
      }
      switch (this.userInfo.VC_ORG$ID) { // 调度以外的用户 如果不是汇总 对用户进行区分
        case 'KMBUR_KMNSEG_ORG_38': // 技术科 只能签收，申请
          this.btnState.deletable = false
          this.btnState.addable = false
          this.btnState.agreeable = false
          this.btnState.disagreeable = false
          this.btnState.qianshou = true
          this.btnState.shangbao = false
          this.btnState.sqYanqi = true
          this.btnState.sqXiaohao = true
          this.btnState.xiafa = false
          break;
        case 'KMBUR_KMNSEG_ORG_48': // 电子车间 签收，下发，申请，上报
          this.btnState.deletable = false
          this.btnState.addable = false
          this.btnState.agreeable = false
          this.btnState.disagreeable = false
          this.btnState.qianshou = true
          this.btnState.shangbao = true
          this.btnState.sqYanqi = true
          this.btnState.sqXiaohao = true
          this.btnState.xiafa = true
          break;
        default:
          if (this.userInfo.VC_PAR$ORG$ID === 'KMBUR_KMNSEG_ORG_48') { // 电子工区
            this.btnState.deletable = false
            this.btnState.addable = false
            this.btnState.agreeable = false
            this.btnState.disagreeable = false
            this.btnState.qianshou = true
            this.btnState.shangbao = true
            this.btnState.sqYanqi = true
            this.btnState.sqXiaohao = true
            this.btnState.xiafa = false
          } else if (this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
            this.btnState.deletable = false
            this.btnState.addable = false
            this.btnState.agreeable = true
            this.btnState.disagreeable = true
            this.btnState.qianshou = true
            this.btnState.shangbao = true
            this.btnState.sqYanqi = true
            this.btnState.sqXiaohao = true
            this.btnState.xiafa = true
          } else {
            switch (this.userInfo.N_LEVEL) {
              case 30: // 现场车间
                this.btnState.deletable = false
                this.btnState.addable = false
                this.btnState.agreeable = true
                this.btnState.disagreeable = true
                this.btnState.qianshou = true
                this.btnState.shangbao = true
                this.btnState.sqYanqi = true
                this.btnState.sqXiaohao = true
                this.btnState.xiafa = true
                break;
              case 10: // 现场工区
                this.btnState.deletable = false
                this.btnState.addable = false
                this.btnState.agreeable = false
                this.btnState.disagreeable = false
                this.btnState.qianshou = true
                this.btnState.shangbao = true
                this.btnState.sqYanqi = true
                this.btnState.sqXiaohao = true
                this.btnState.xiafa = false
                break;
              default:
                this.btnState.deletable = false
                this.btnState.addable = false
                this.btnState.agreeable = false
                this.btnState.disagreeable = false
                this.btnState.qianshou = true
                this.btnState.shangbao = true
                this.btnState.sqYanqi = true
                this.btnState.sqXiaohao = true
                this.btnState.xiafa = false
                break;
            }
          }
          break;
      }
    }
    switch (this.queryParam.tabtype) {  // 将不属于当前tab页的内容过滤
      case '2': // 本部门协助 
        this.btnState.qianshou = false
        this.btnState.shangbao = false
        this.btnState.sqYanqi = false
        this.btnState.sqXiaohao = false
        break;
      case '3': // 未整改
      case '4': // 已销号
      case '6': // 汇总表 
        this.btnState.qianshou = false
        this.btnState.shangbao = false
        this.btnState.sqYanqi = false
        this.btnState.sqXiaohao = false
        this.btnState.addable = false
        this.btnState.agreeable = false
        this.btnState.disagreeable = false
        break;
    }

  }


  initOptConfig() {
    const deleteCfg = {
      name: '删除',
      value: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'],
      func: (row) => {
        return this.jzjcMainService.delete(row.id);
      },
    };
    const shangbaoCfg = {
      name: '上报',
      value: ['2', '5', '6', '9'],
      func: (row) => {
        if (!this.confirmParam.handleinfo) {
          this.msg.warning('请填写上报情况');
          return false;
        }
        let status = '3';
        switch (this.userInfo.VC_ORG$ID) {
          case 'KMBUR_KMNSEG_ORG_48': // 电子车间
            status = '13'
            break;
          default:
            if (this.userInfo.VC_PAR$ORG$ID === 'KMBUR_KMNSEG_ORG_48') { // 电子工区
              status = '12'
            } else if (this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
              status = '3'
            } else {
              switch (this.userInfo.N_LEVEL) {
                case 30: // 现场车间
                  status = '3'
                  break;
                case 10: // 现场工区
                  status = '12'
                  break;
                default:
                  break;
              }
            }
            break;
        }

        const param = {
          handleinfo: this.confirmParam.handleinfo,
          status,
          id: row.id,
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const qianshouCfg = {
      name: '签收',
      value: ['1', '11', '3', '12', '13'],
      func: (row) => {
        const param = {
          status: '2',
          id: row.id,
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const sqYanqiCfg = {
      name: '申请延期',
      value: ['2', '5', '6', '9'],
      func: (row) => {
        if (!this.confirmParam.handleinfo) {
          this.msg.warning('请填写延期原因');
          return false;
        }
        let applyrectdate = "";
        if (!this.confirmParam._applyrectdate) {
          this.msg.warning('请填写延期日期');
          return false;
        } else {
          applyrectdate = this.datePipe.transform(this.confirmParam._applyrectdate, 'yyyy-MM-dd')
        }
        const param = {
          status: '4',
          id: row.id,
          handleinfo: this.confirmParam.handleinfo,
          applyrectdate
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const sqXiaohaoCfg = {
      name: '申请销号',
      value: ['2', '5', '6', '9'],
      func: (row) => {
        const param = {
          status: '7',
          id: row.id,
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const boHuiCfg = {
      name: '驳回',
      value: ['4', '7'],
      func: (row) => {
        if (!row.status) {
          return;
        }
        if (!this.confirmParam.handleinfo) {
          this.msg.warning('请填写意见');
          return false;
        }
        const bhStatusMap = {
          '4': '6',
          '7': '9'
        }
        const param = {
          status: bhStatusMap[row.status],
          id: row.id,
          handleinfo: this.confirmParam.handleinfo,
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const tongYiCfg = {
      name: '同意',
      value: ['4', '7'],
      func: (row) => {
        if (!row.status) {
          return;
        }
        if (!this.confirmParam.handleinfo) {
          this.msg.warning('请填写意见');
          return false;
        }
        const tyStatusMap = {
          '4': '5',
          '7': '8'
        }
        const param = {
          status: tyStatusMap[row.status],
          id: row.id,
          handleinfo: this.confirmParam.handleinfo,
        };
        return this.jzjcMainService.edit(param);
      },
    };
    const xiafaCfg: any = { name: '下发', value: ['2'] }
    xiafaCfg.func = (row) => {
      if (!row.status) {
        return;
      }
      let status = '';
      status = this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40' ? '1' : '11'
      const param = {
        status,
        id: row.id,
      };
      return this.jzjcMainService.edit(param);
    };
    this.optConfig = {
      'delete': deleteCfg,
      'qianshou': qianshouCfg,
      'shangbao': shangbaoCfg,
      'sqYanqi': sqYanqiCfg,
      'sqXiaohao': sqXiaohaoCfg,
      'boHui': boHuiCfg,
      'tongYi': tongYiCfg,
      'xiafa': xiafaCfg,
    }
  }

  selfCheck(optType, rows?) {
    let res = true;
    switch (optType) {
      case 'delete':
        res = this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40' ? true : false;
        if (res === false) {
          this.msg.warning(`您不具备该操作权限`);
        }
        break;
      case 'boHui':
      case 'tongYi':
        const optName1 = this.optConfig[optType].name;
        for (const row of rows) {
          if (this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
            // 因不能得到其父节点的名称 暂时不对质检组进行正常自检提示
            if (this.userInfo.VC_PAR$ORG$ID === row.chargeorg) {
              this.msg.warning(`您不能${optName1}责任部门为自身的条目`);
              return false;
            }
          } else {
            if (row.chargeorg === this.userInfo.VC_ORG$ID) {
              this.msg.warning(`您不能${optName1}责任部门为自身的条目`);
              return false;
            }
          }
        }

        break;
      case 'qianshou':
      case 'shangbao':
      case 'sqYanqi':
      case 'sqXiaohao':
        const optName = this.optConfig[optType].name;
        for (const row of rows) {
          if (this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1) {
            // 因不能得到其父节点的名称 暂时不对质检组进行正常自检提示
            if (this.userInfo.VC_PAR$ORG$ID !== row.chargeorg) {
              this.msg.warning(`您不能操作责任部门为${row.chargeorgname}的条目`);
              return false;
            }
          } else {
            if (row.chargeorg !== this.userInfo.VC_ORG$ID) {
              this.msg.warning(`您只能${optName}责任部门为${this.userInfo.VC_ORG$NAME}的条目`);
              return false;
            }
          }
        }
        break;
      default:
        break;
    }
    return res;
  }

  checkAll(v) {
    this.tableData.forEach(row => {
      row.checked = v;
    });
  }

  export() {
    let param = '';
    let time = '';
    if (this.queryParam.station) {
      param += '&station=' + this.queryParam.station;
    }
    if (this.queryParam.chargeorg) {
      param += '&chargeorg=' + this.queryParam.chargeorg;
    }
    if (this.queryParam.problemtypeid) {
      param += '&problemtypeid=' + this.queryParam.problemtypeid;
    }
    if (this.queryParam.status) {
      param += '&status=' + this.queryParam.status;
    }
    if (this.queryParam._startdate) {
      param += '&startdate=' + this.datePipe.transform(this.queryParam._startdate, 'yyyy-MM-dd');
      time += this.datePipe.transform(this.queryParam._startdate, 'yyyy-MM-dd');
    }
    if (this.queryParam._enddate) {
      param += '&enddate=' + this.datePipe.transform(this.queryParam._enddate, 'yyyy-MM-dd');
      if (this.queryParam._startdate) {
        time += '~~';
      }
      time += this.datePipe.transform(this.queryParam._enddate, 'yyyy-MM-dd');
    }
    if (param) {
      param = param.replace('&', '?');
    }
    this.exportFile.getDown(environment.project + '/jzjcProblem/dataExcel' + param, this.exportTitle + time + '.xls');
  }


  statusFollow(data) {
    switch (data.cancle) {
      case '3':// 逾期未整改
        return this.statusFollowService.init('jzjc_main_status', '4');
      default:
        return this.statusFollowService.init('jzjc_main_status', data.status);
    }
  }

  query() {
    const param: any = {
      page: this.pagination.pageIndex,
      limit: this.pagination.selectedValue,
    };

    Object.assign(param, this.queryParam);
    if (param.status === "cancle_3") {
      delete param.status;
      param.cancle = 3;
    }
    if (param._startdate) {
      param.startdate = this.datePipe.transform(param._startdate, 'yyyy-MM-dd')
    }
    if (param._enddate) {
      param.enddate = this.datePipe.transform(param._enddate, 'yyyy-MM-dd')
    }
    this.jzjcTool.removePrivateFields(param);
    this.jzjcTool.removeUselessFields(param);
    this.tableLoading = true;
    this.jzjcMainService.query(param).subscribe(res => {
      this.tableLoading = false;
      if (res && res.data) {
        res.data.forEach(row => {
          row._handleinfo = this.doHandleinfo(row.handleinfo);
        });
        this.tableData = res.data;
        for (const item of this.tableData) {
          if (item.cancle == '3') {// 逾期未整改
            item.newStatusText = this.jzjcTool.status.statusList[item.status] + ' (逾期)';
          } else {
            item.newStatusText = this.jzjcTool.status.statusList[item.status];
          }
        }
        this.pagination.pageTotal = res.count;
      }
    });
  }

  doHandleinfo(handleinfo) {
    if (handleinfo) {
      const handles = handleinfo.split('<br>');
      const arr = handles.filter(el => el);
      const arr1 = arr.map((info, i) => {
        const index = this.jzjcTool.numToCn(i + 1);
        return `${index}、` + info;
      });
      return this.jzjcTool.replaceBr(arr1.join('<br>'));
    } else {
      return handleinfo;
    }
  }



  getMode(row?) {
    const mode = {
      main: '',
      zg: '',
    }

    if (!row || row === 'add') {
      mode.main = 'add';
    } else {
      if (this.userInfo.VC_ORG$ID === 'KMBUR_KMNSEG_ORG_40') {// 调度
        mode.main = ['0', '3'].includes(row.status) ? 'edit' : 'edit_notBm';
        mode.zg = 'detail';
      } else {
        if (this.queryParam.pc === 'all') {
          mode.main = 'detail';
          mode.zg = 'detail';
          return mode;
        }
        if (row.chargeorg === this.userInfo.VC_ORG$ID) { // 本人是责任部门
          switch (this.userInfo.VC_ORG$ID) {
            case 'KMBUR_KMNSEG_ORG_38': // 技术科
              mode.main = 'edit_onlyZrr';
              mode.zg = ['2', '5', '6', '9'].includes(row.status) ? 'edit' : 'detail';
              break;
            case 'KMBUR_KMNSEG_ORG_48': // 电子车间
              if (['2', '5', '6', '9'].includes(row.status)) {
                mode.main = 'edit_onlyBm';
                mode.zg = 'edit';
              } else {
                mode.main = 'detail';
                mode.zg = 'detail';
              }
              break;
            default:
              if (this.userInfo.VC_PAR$ORG$ID === 'KMBUR_KMNSEG_ORG_48') { // 电子工区
                mode.main = 'detail';
                mode.zg = ['2', '5', '6', '9'].includes(row.status) ? 'edit' : 'detail';
              } else {
                switch (this.userInfo.N_LEVEL) {
                  case 30: // 现场车间
                    if (['2', '5', '6', '9'].includes(row.status)) {
                      mode.main = 'edit_onlyBm';
                      mode.zg = 'edit';
                    } else {
                      mode.main = 'detail';
                      mode.zg = 'detail';
                    }
                    break;
                  case 10: // 现场工区
                    mode.main = 'detail';
                    mode.zg = ['2', '5', '6', '9'].includes(row.status) ? 'edit' : 'detail';
                    break;

                  default:
                    break;
                }
              }
              break;
          }
        } else if (this.userInfo.VC_ORG$NAME.indexOf('质检组') !== -1 && this.userInfo.VC_PAR$ORG$ID === row.chargeorg) {
          if (['2', '5', '6', '9'].includes(row.status)) {
            mode.main = 'edit_onlyBm';
            mode.zg = 'edit';
          } else {
            mode.main = 'detail';
            mode.zg = 'detail';
          }
        } else if (row.assistorg && row.assistorg.indexOf(this.userInfo.VC_ORG$ID) !== -1) { // 本人是协助部门
          mode.main = 'detail';
          mode.zg = ['2', '5', '6', '9'].includes(row.status) ? 'edit' : 'detail';
        } else {
          mode.main = 'detail';
          mode.zg = 'detail';
        }
      }
    }

    return mode;
  }


  showDetail(row?) {
    this.detail = {};
    const mode = this.getMode(row);
    const detail: any = {};
    detail.data = {};
    detail.mode = mode.main;
    detail.zgMode = mode.zg;
    detail.pc = this.queryParam.pc;
    this.modal.visible = true;
    if (row) {
      detail.data = row;
    }
    switch (detail.mode) {
      case 'add':
        this.modal.nzBodyStyle = { width: '50vw', minWidth: '600px' };
        this.modal.title = '新增';
        break;
      case 'edit':
      case 'edit_notBm':
      case 'edit_onlyBm':
      case 'edit_onlyBms':
      case 'edit_onlyZrr':
        this.modal.nzBodyStyle = { width: '80vw', minWidth: '800px' };
        this.modal.title = '修改';
        break;
      default:
        this.modal.nzBodyStyle = { width: '80vw', minWidth: '800px' };
        this.modal.title = '详情';
        break;
    }
    this.detail = detail;
  }


  delete(row) {
    if (!row.id) {
      return;
    }
    this.modalService.confirm({
      nzTitle: `<i>确认</i>`,
      nzWidth: '600',
      nzContent: '确认删除吗？',
      nzOnOk: () => {
        this.jzjcMainService.delete(row.id).subscribe(res => {
          if (res && res.msg === 'Ok') {
            this.msg.success('删除成功');
            this.detailCancel();
            this.query();
          } else {
            this.msg.warning('删除失败');
          }
        });
      }
    });
  }

  detailCancel() {
    this.modal.visible = false;
  }





  /**
   * 不能处理
   * @param problemid 
   */
  canNotHandle(problemid) {
    if (!problemid) {
      return;
    }
    this.jzjcMainService.canNotHandle(problemid).subscribe(res => {
      if (res && res.msg === 'Ok') {
        this.msg.success('反映成功');
      } else {
        this.msg.warning('反映失败');
      }
    })
  }

  /**
   * 申请销号
   * @param problemid 
   */
  sqxhZgqk(problemid) {
    if (!problemid) {
      return;
    }
    this.jzjcMainService.sqxhZgqk(problemid).subscribe(res => {
      if (res && res.msg === 'Ok') {
        this.msg.success('申请成功');
      } else {
        this.msg.warning('申请失败');
      }
    })
  }

  /**
   * 销号
   * @param problemid 
   */
  xhZgqk(problemid) {
    if (!problemid) {
      return;
    }
    this.jzjcMainService.xhZgqk(problemid).subscribe(res => {
      if (res && res.msg === 'Ok') {
        this.msg.success('销号成功');
      } else {
        this.msg.warning('销号失败');
      }
    })
  }

  getChecked() {
    const delRows = this.tableData.filter(row => row.checked);
    return delRows;
  }

  /**
   * 操作检验
   * @param optType 
   * @param checkedRows 
   */
  optCheck(optType, checkedRows) {
    let check = true;
    const optName = this.optConfig[optType].name;
    if (!checkedRows.length) {
      this.msg.warning(`请先勾选条目以执行 ${optName} 操作`);
      check = false;
    } else {
      const optSta = this.optConfig[optType].value;
      const index = checkedRows.findIndex(row => {
        return !optSta.includes(row.status);
      })
      const statusList = this.jzjcTool.status.statusList;
      const qsStaStr = optSta.map(el => statusList[el]);
      if (index !== -1) {
        this.msg.warning(`您只能${optName}状态为: ${qsStaStr.join('、')} 的条目，勾选项中存在不可${optName}项`,
          { nzDuration: 5000 });
        check = false;
      }

      if (this.selfCheck(optType, checkedRows) === false) {
        return false;
      }
    }
    return check;
  }


  optAll(optType, content?) {
    const checkedRows = this.getChecked();
    const optName = this.optConfig[optType].name;
    const check = this.optCheck(optType, checkedRows);
    if (!check) {
      return;
    }
    this.confirmParam = {};
    this.modalService.confirm({
      nzTitle: `确认${optName}勾选项？`,
      nzWidth: '600',
      nzContent: content || "",
      nzOnOk: () => {
        const obs = [];
        let paramCheck: any = true;
        checkedRows.forEach(row => {
          const ob = this.optConfig[optType].func(row);
          if (ob === false) {
            paramCheck = false;
          } else {
            obs.push(ob);
          }
        });
        if (paramCheck === false) {
          return false;
        }
        forkJoin(obs).subscribe(resArr => {
          const allRes = resArr.every(res => {
            if (!res || res.msg !== 'Ok') {
              return false;
            }
            return true;
          });
          if (allRes) {
            this.msg.success(`${optName}成功`);
            this.query();
          } else {
            this.msg.success(`${optName}失败`);
          }
        })
      }
    })
  }


  detailClick(v) {
    switch (v) {
      case 'close':
        this.detailCancel();
        break;
      case 'close_load':
        this.detailCancel();
        this.query();
        break;
      case 'load':
        this.query();
        break;
      default:
        break;
    }

  }

  onTabIndexChange(v) {
    if (!this.tabs[v].tabId) {
      return;
    }
    if (this.tabs[v].key) {
      delete this.queryParam.tabtype;
      this.queryParam[this.tabs[v].key] = this.tabs[v].tabId;
    } else {
      this.queryParam.tabtype = this.tabs[v].tabId;
      this.jzjcTool.initStatus(this.queryParam.tabtype);
    }

    this.initBtnState();
    this.queryParam.status = "";
    this.queryParam.chargeorg = "";
    this.queryParam.problemtypeid = "";

    this.queryParam.station = "";
    this.queryParam._startdate = "";
    this.queryParam._enddate = "";


    this.query();
  }

  judgeScreen() {
    const screenHeight = screen.height;
    if (screenHeight <= 720) {
      this.screenType = '3';
    } else if (screenHeight <= 900) {
      this.screenType = '2';
    } else {
      this.screenType = '1';
    }
  }
}
